
package com.ln.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ln.store.domain.User;
import com.ln.store.service.UserService;
import com.ln.store.service.Impl.UserServiceImpl;
import com.ln.store.utils.MailUtils;
import com.ln.store.utils.MyBeanUtils;
import com.ln.store.utils.UUIDUtils;
import com.ln.store.web.base.BaseServlet;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends BaseServlet {
	
	
	/**
	 * 点击注册跳转到注册页面
	 * @param request
	 * @param response
	 * @return
	 */
	public String registerUI(HttpServletRequest request,HttpServletResponse response) {
		return "/jsp/register.jsp";
	}
	
	/**
	 * 点击注册按钮
	 * @param request
	 * @param response
	 * @return
	 */
	public String userRegister(HttpServletRequest request,HttpServletResponse response) {
		
		try {
			//接收表单餐
			Map<String, String[]> map = request.getParameterMap();
			User user = new User();
			
			user.setUid(UUIDUtils.getId());
			user.setState(0);
			user.setCode(UUIDUtils.getCode());
			MyBeanUtils.populate(user, map);
			
			//遍历map
//			Set<String> keySet = map.keySet();
//			Iterator<String> iterator = keySet.iterator();
//			while (iterator.hasNext()) {
//				String key = (String) iterator.next();
//				System.out.println(key);
//				String[] strings = map.get(key);
//				for (String string : strings) {
//					System.out.println(string);
//				}
//			}
			//调用业务层注册功能
			UserService service = new UserServiceImpl();
			service.userRegister(user);
			//注册成功，向用户邮箱发送信息，跳转到提示页面
			//发送邮件
			MailUtils.sendMail(user.getEmail(), user.getCode());
			request.setAttribute("msg", "注册成功,请激活账号");
			//注册失败，跳转到提示页面
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("msg", "注册失败");
		}
		return "/jsp/info.jsp";
	}
	
	/**
	 * 
	 * 用户激活
	 * @param request
	 * @param response
	 * @return
	 */
	public String active(HttpServletRequest request,HttpServletResponse response) {

		try {
			// 获取code参数
			String code = request.getParameter("code");
			// 调用业务层激活功能
			UserService service = new UserServiceImpl();
			boolean flag = service.userActive(code);
			if (flag) {
				// 激活成功提示用户 跳转登录页面
				request.setAttribute("msg", "激活成功！请登录");
				return "/jsp/login.jsp";
			} else {
				// 激活失败提示用户 跳转到信息页面
				request.setAttribute("msg", "激活失败！请重新激活！");
				return "/jsp/info.jsp";
			}
		} catch (Exception e) {
			// 激活失败提示用户 跳转到信息页面
			e.printStackTrace();
			request.setAttribute("msg", "激活失败！请重新激活！");
			return "/jsp/info.jsp";
		}
	}
	
	/**
	 * 登录界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public String loginUI(HttpServletRequest request,HttpServletResponse response) {
		
		return "/jsp/login.jsp";
	}
	
	/**
	 * 登录功能
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public String userLogin(HttpServletRequest request,HttpServletResponse response) {
		
		 try {
			//获取提交的账号和密码
			 User user = new User();
			 MyBeanUtils.populate(user, request.getParameterMap());
			 
			 System.out.println(user.getUsername()+user.getPassword());
			 //调用业务层的登录功能
			 UserService service = new UserServiceImpl();
			 User user2 = service.userLogin(user);
			 //保存用户到sesstion
			 request.getSession().setAttribute("loginUser", user2);
			 //登录成功 重定向到
			 response.sendRedirect("/store_v5/index.jsp");
			 return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			//登录不成功 提示用户
			request.setAttribute("msg", e.getMessage());
			return "/jsp/login.jsp";
		}
	}
	
	/**
	 * 退出功能
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public String logOut(HttpServletRequest request,HttpServletResponse response) {
		try {
			//清空sesstion
			request.getSession().invalidate();
			//重定向到首页
			response.sendRedirect("/store_v5/index.jsp");
			
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
